package handle

import (
	"net/http"
	"net/url"
	"regexp"
	"strconv"
)

func LoginHandle(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	checkLoginParam(r.Form)
}

func checkLoginParam(form url.Values) *Result {
	err := NewResult()

	username := form.Get("username")
	if len(username) == 0 {
		err.Result = UsernameIsNULL
		err.Message = ErrMessage[UsernameIsNULL]
		return err
	}

	match, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, username)
	if !match {
		err.Result = UsernameIsInvalid
		err.Message = ErrMessage[UsernameIsInvalid]
		return err
	}

	passwd := form.Get("passwd")
	len := len(passwd)
	if len == 0 {
		err.Result = PasswdIsNULL
		err.Message = ErrMessage[PasswdIsNULL]
		return err
	}

	channel := form.Get("channel")
	if len(channel) == 0 {
		err.Result = ChannelIsInvalid
		err.Message = ErrMessage[ChannelIsInvalid]
		return err
	}

	platform, _ := strconv.Atoi(form.Get("platform"))
	if platform < 0 {
		err.Result = PlatformIsInvalid
		err.Message = ErrMessage[PlatformIsInvalid]
	}

	return nil
}
